﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Petroleum.UI.Tool.Office
{
    /// <summary>
    /// 初始化和设置“AxDSOFramer.AxFramerControl”控件，在线编辑office（内嵌office的效果）
    /// </summary>
    class AxFramerControlSetter
    {
        private AxDSOFramer.AxFramerControl m_axFramerControl; //在线编辑office的控件
        private bool isInit;
        public bool IsInit
        {
            get { return isInit; }
            set { value = isInit; }
        }
        public AxFramerControlSetter(AxDSOFramer.AxFramerControl m_axFramerControl)
        {
            this.m_axFramerControl = m_axFramerControl;
            this.isInit = false;
        }

        /// <summary>
        /// 关闭时的工作
        /// </summary>
        public void Close()
        {
            this.m_axFramerControl.Close();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="_ExcelFilePath"></param>
        public void Init(string _ExcelFilePath, Panel panel1)
        {
            try
            {
                this.isInit = true;

                // 不能在此处注册的原因，是因为：注册时，新开了一个进程，此处不好控制。因此，必须在加载前，确认已经注册完成。
                ////确认dsoframer.ocx已经在本地注册过
                //if (!OcxRegister.IsRegisted())
                //{
                //    OcxRegister.RegisterMain();//注册dsoframer.ocx
                //}

                if (!File.Exists(_ExcelFilePath))
                {
                    MessageBox.Show("文件不存在或未标识的文件格式！", "提示信息");
                    return;
                    //throw new ApplicationException("文件不存在或未标识的文件格式！");
                }

                AddAxFramerControl(panel1);//加载填充控件

                m_axFramerControl.Titlebar = false;//是否显示excel标题栏
                m_axFramerControl.Menubar = false;//是否显示excel的菜单栏
                m_axFramerControl.Toolbars = false;//是否显示excel的工具栏

                InitOfficeControl(_ExcelFilePath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 向panel填充AxFramerControl控件
        /// </summary>
        /// <param name="panel1"></param>
        private void AddAxFramerControl(Panel panel1)
        {
            try
            {
                panel1.Controls.Add(m_axFramerControl);
                m_axFramerControl.Dock = DockStyle.Fill;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 初始化office控件,加载Excel/Word/PPT
        /// </summary>
        /// <param name="_sFilePath"></param>
        private void InitOfficeControl(string _sFilePath)
        {
            try
            {
                if (m_axFramerControl == null)
                {
                    throw new ApplicationException("请先初始化office控件对象！");
                }

                //this.m_axFramerControl.SetMenuDisplay(48);
                //这个方法很特别，一个组合菜单控制方法，我还没有找到参数的规律，有兴趣的朋友可以研究一下
                string sExt = System.IO.Path.GetExtension(_sFilePath).Replace(".", "");
                //this.m_axFramerControl.CreateNew(this.LoadOpenFileType(sExt));//创建新的文件
                this.m_axFramerControl.Open(_sFilePath, false, this.LoadOpenFileType(sExt), "", "");//打开文件
                //隐藏标题
                this.m_axFramerControl.Titlebar = false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //下面这个方法是dso打开文件时需要的一个参数，代表office文件类型
        /// <summary>
        /// 根据后缀名得到打开方式
        /// </summary>
        /// <param name="_sExten"></param>
        /// <returns></returns>
        private string LoadOpenFileType(string _sExten)
        {
            try
            {
                string sOpenType = "";
                switch (_sExten.ToLower())
                {
                    case "xls":
                        sOpenType = "Excel.Sheet";
                        break;
                    case "xlsx":
                        sOpenType = "Excel.Sheet";
                        break;
                    case "doc":
                        sOpenType = "Word.Document";
                        break;
                    case "docx":
                        sOpenType = "Word.Document";
                        break;
                    case "ppt":
                        sOpenType = "PowerPoint.Show";
                        break;
                    case "pptx":
                        sOpenType = "PowerPoint.Show";
                        break;
                    case "vsd":
                        sOpenType = "Visio.Drawing";
                        break;
                    default:
                        sOpenType = "Word.Document";
                        break;
                }
                return sOpenType;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
